﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.IO;
using ExamAnalysis.Core;
using ExamAnalysis.Model;
using System.Web.Security;

namespace ExamAnalysis.Manage
{
    public partial class UserInRole : System.Web.UI.Page
    {
        static string roleid = "";
        static string userid = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var myRole = from role in db.aspnet_Roles
                             select new { RoleId = role.RoleId.ToString(), RoleName = role.RoleName };
                var myUser = from user in db.aspnet_Users
                             select new { UserId = user.UserId.ToString(), UserName = user.UserName };
                this.Store3.DataSource = myRole;
                this.Store3.DataBind();
                this.Store1.DataSource = myUser;
                this.Store1.DataBind();
            }
                       
            if (roleid != "")
            {
                DataBind();
            }
        }

        protected void DataBind()
        {
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var myUIR = from ur in db.aspnet_UsersInRoles
                            where ur.aspnet_Roles.RoleName.ToString() == roleid
                            select new { UserId = ur.UserId.ToString(), UserName = ur.aspnet_Users.UserName, RoleName = ur.aspnet_Roles.RoleName };
                this.Store2.DataSource = myUIR;
                this.Store2.DataBind();
            }
        }

        protected void Button_Click(object sender, Ext.Net.DirectEventArgs e)
        {
           RowSelectionModel sm = this.GridPanel3.GetSelectionModel() as RowSelectionModel;
            if (sm.SelectedRows.Count != 0)
            {
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    roleid = row.RecordID.ToString();
                }
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var myRole = from role in db.aspnet_Roles
                                 where role.RoleName == roleid
                                 select new { RoleId = role.RoleId.ToString(), RoleName = role.RoleName };
                    DropDownField1.Text = myRole.First().RoleName.ToString();
                }
                DataBind();
            }
            else
            {
               X.Msg.Alert("提示信息", "请选择一组").Show();
            }
        }

        protected void Button2_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            if (roleid == "")
            {
                X.Msg.Alert("提示信息", "请选择一个组").Show();
            }
            else
           {
                userid = "";
                this.FormWindow.Visible = true;
                this.FormWindow.Show();
                Button6.Show();
            }
        }

        protected void Button4_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel2.GetSelectionModel() as RowSelectionModel;
            if (sm.SelectedRows.Count != 0)
            {
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    userid = row.RecordID.ToString();
                }
                Roles.RemoveUserFromRole(userid, roleid);
                    X.Msg.Alert("提示信息", "删除成功").Show();
                    DataBind();
            
            }
            else
            {
                X.Msg.Alert("提示信息", "请选择一个用户").Show();
            }
        }

        protected void Add_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel1.GetSelectionModel() as RowSelectionModel;
            if (sm.SelectedRows.Count != 0)
           {
                foreach (SelectedRow row in sm.SelectedRows)
               {
                    userid = row.RecordID.ToString();
                }
                if(!Roles.IsUserInRole(userid,roleid))
                        Roles.AddUserToRole(userid,roleid);
                        X.Msg.Alert("提示信息", String.Format("成功添加用户")).Show();
                        userid = "";
                        roleid = "";
                        Response.Redirect("UserInRole.aspx");
            }
            else
            {
                X.Msg.Alert("提示信息", "请选择一个用户").Show();
            }
        }
    }
}